package org.oschina.api.blog.blog.controller;

import java.util.List;

import org.oschina.api.BaseController;
import org.oschina.api.blog.blog.model.Blog;
import org.oschina.api.blog.blog.service.BlogService;
import org.oschina.api.blog.blog.vo.BlogAccessVo;
import org.oschina.api.blog.blog.vo.BlogVo;
import org.oschina.bean.Pager;
import org.oschina.bean.Result;
import org.oschina.config.Config;
import org.oschina.handler.validator.annotation.Validate;
import org.oschina.handler.validator.annotation.ValidateField;
import org.oschina.handler.validator.filter.HtmlCleanFileter;
import org.oschina.handler.validator.filter.TextOnlyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value=Config.API_PATH+"/blog")
public class BlogController extends BaseController {

	@Autowired
	private BlogService blogService;
	
	/**
	 * 关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/create",method=RequestMethod.POST)
	public Result createFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return blogService.createFavorite(id);
	}
	
	/**
	 * 删除关注
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/favorite/delete",method=RequestMethod.POST)
	public Result deleteFavorite
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return blogService.deleteFavorite(id);
	}
	
	/**
	 * 根据搜索返回
	 * @param keywords
	 * @param first
	 * @param max
	 * @return
	 */
	@Validate
	@RequestMapping(value="/search",method=RequestMethod.GET)
	public Pager<BlogVo> findVoBySearch
	(
		@Validate(validateField=@ValidateField(filter={TextOnlyFilter.class})) String keyword,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return blogService.findVoBySearch(keyword, pn);
	}
	
	/**
	 * 根据作者返回最新
	 * @param author
	 * @return
	 */
	@Validate
	@RequestMapping(value="/user/{author}/new",method=RequestMethod.GET)
	public List<BlogVo> findNewByAuthor
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer author
	)
	{
		return blogService.findNewByAuthor(author);
	}
	
	/**
	 * 根据作者返回
	 * @param author
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/user/{author}",method=RequestMethod.GET)
	public Pager<BlogVo> findByAuthor
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer author,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return blogService.findByAuthor(author, pn);
	}
	
	/**
	 * 根据作者和目录返回
	 * @param author
	 * @param catalog
	 * @param pn
	 * @return
	 */
	@Validate
	@RequestMapping(value="/user/{author}/catalog/{catalog}",method=RequestMethod.GET)
	public Pager<BlogVo> findByAuthorAndCatalog
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer author,
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer catalog,
		Integer pn
	)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return blogService.findByAuthorAndCatalog(author, catalog, pn);
	}
	
	/**
	 * 查看博客入口
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	public BlogAccessVo findById
	(
		@PathVariable @Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id
	)
	{
		return blogService.findById(id);
	}
	
	/**
	 * 发表评论
	 * @param id
	 * @param content
	 * @return
	 */
	@Validate
	@RequestMapping(value="/comment/create",method=RequestMethod.POST)
	public Result createComment
	(
		@Validate(validateField=@ValidateField(validateValue=true,min=1)) Integer id,
		String content,
		Integer quote
	)
	{
		if(quote==null || quote<0)
			quote=0;
		
		return blogService.createComment(id, content, quote);
	}
	
	/**
	 * 返回推荐
	 * @param pn
	 * @return
	 */
	
	@RequestMapping(value="/recommend",method=RequestMethod.GET)
	public Pager<BlogVo> findVoByRecommend(Integer pn)
	{
		if(pn==null || pn<=0)
			pn=1;
		
		return blogService.findVoByRecommend(pn);
	}
	
	/**
	 * 创建
	 * @param blog
	 * @return
	 */
	@Validate
	@RequestMapping(value="/create",method=RequestMethod.POST)
	public Result create
	(
		@Validate(validateField=
		{
			@ValidateField(name="title",filter={TextOnlyFilter.class}),
			@ValidateField(name="content",filter={HtmlCleanFileter.class}),
			@ValidateField(name="order",validateValue=true,min=1),
			@ValidateField(name="catalog",validateValue=true,min=1),
			@ValidateField(name="comment"),
			@ValidateField(name="oneself")
		}) Blog blog
	)
	{
		return blogService.create(blog);
	}
	
	/**
	 * 编辑
	 * @param blog
	 * @return
	 */
	@Validate
	@RequestMapping(value="/edit/{id}",method=RequestMethod.POST)
	public Result edit
	(
		@Validate(validateField=
		{
			@ValidateField(name="id",validateValue=true,min=1),
			@ValidateField(name="title",filter={TextOnlyFilter.class}),
			@ValidateField(name="content",filter={HtmlCleanFileter.class}),
			@ValidateField(name="order",validateValue=true,min=1),
			@ValidateField(name="catalog",validateValue=true,min=1),
			@ValidateField(name="comment"),
			@ValidateField(name="oneself")
		}) Blog blog
	)
	{
		return blogService.edit(blog);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/delete/{id}",method=RequestMethod.POST)
	public Result delete
	(
		@PathVariable @Validate(validateField=@ValidateField(name="id",validateValue=true,min=1)) Integer id
	)
	{
		return blogService.delete(id);
	}
	
	/**
	 * 设置/取消推荐
	 * @param id
	 * @return
	 */
	@Validate
	@RequestMapping(value="/setRecommend/{id}",method=RequestMethod.POST)
	public Result setRecommendtime
	(
		@PathVariable @Validate(validateField=@ValidateField(name="id",validateValue=true,min=1)) Integer id
	)
	{
		return blogService.setRecommend(id);
	}
}